package com.nuaa.likesystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuaa.likesystem.constant.ThumbConstant;
import com.nuaa.likesystem.domain.ThumbDomain;
import com.nuaa.likesystem.mapper.BlogMapper;
import com.nuaa.likesystem.mapper.ThumbMapper;
import com.nuaa.likesystem.model.entity.Blog;
import com.nuaa.likesystem.model.entity.Thumb;
import com.nuaa.likesystem.model.entity.User;
import com.nuaa.likesystem.model.vo.BlogVO;
import com.nuaa.likesystem.service.BlogService;
import com.nuaa.likesystem.service.ThumbService;
import com.nuaa.likesystem.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 11352
 * @description 针对表【blog】的数据库操作Service实现
 * @createDate 2025-04-20 17:51:36
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {


	@Resource
	private UserService userService;

	@Resource
	private ThumbMapper thumbMapper;

	@Resource
	private RedisTemplate<String, Object> redisTemplate;

	@Resource
	private BlogMapper blogMapper;

	@Resource
	private ThumbDomain thumbDomain;

	@Override
	public BlogVO getBlogVOById(long blogId, HttpServletRequest request) {
		Blog blog = blogMapper.selectById(blogId);
		User longinUser = userService.getLoginUser(request);
		return getBlogVO(blog, longinUser);
	}

	@Override
	public List<BlogVO> getBlogVOList(List<Blog> blogList, HttpServletRequest request) {
		User loginUser = userService.getLoginUser(request);
		Map<Long, Boolean> blogIdHasThumbMap = new HashMap<>();
		//if (ObjUtil.isNotEmpty(loginUser)) {
		//	Set<Long> blogIdSet = blogList.stream()
		//	                              .map(Blog::getId)
		//	                              .collect(Collectors.toSet());
		//	// 获取点赞
		//	thumbMapper.selectList(new LambdaQueryWrapper<Thumb>().in(Thumb::getBlogId, blogIdSet)
		//	                                                      .eq(Thumb::getUserId, loginUser.getId()))
		//	           .forEach(blogThumb -> blogIdHasThumbMap.put(blogThumb.getBlogId(), true));
		//}

		if (ObjUtil.isNotEmpty(loginUser)) {
			List<Object> blogIdList = blogList.stream()
			                                  .map(blog -> blog.getId()
			                                                   .toString())
			                                  .collect(Collectors.toList());
			// 获取点赞
			List<Object> thumbList = redisTemplate.opsForHash()
			                                      .multiGet(ThumbConstant.USER_THUMB_KEY_PREFIX + loginUser.getId(), blogIdList);
			for (int i = 0; i < thumbList.size(); i++) {
				if (thumbList.get(i) == null) {
					continue;
				}
				blogIdHasThumbMap.put(Long.valueOf(blogIdList.get(i)
				                                             .toString()), true);
			}
		}

		return blogList.stream()
		               .map(blog -> {
			               BlogVO blogVO = BeanUtil.copyProperties(blog, BlogVO.class);
			               blogVO.setHasThumb(blogIdHasThumbMap.get(blog.getId()));
			               return blogVO;
		               })
		               .toList();
	}

	private BlogVO getBlogVO(Blog blog, User loginUser) {
		BlogVO blogVO = new BlogVO();
		BeanUtils.copyProperties(blog, blogVO);

		if (Objects.isNull(loginUser)) {
			return blogVO;
		}
		//Thumb thumb = thumbMapper.selectOne(new LambdaQueryWrapper<Thumb>().eq(Thumb::getBlogId, blog.getId())
		//                                                                   .eq(Thumb::getUserId, longinUser.getId()));
		//
		//blogVO.setHasThumb(Objects.nonNull(thumb));
		Boolean exist = thumbDomain.hasThumb(blog.getId(), loginUser.getId());
		blogVO.setHasThumb(exist);
		return blogVO;
	}
}




